package com.parkhome.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

/**
 * 工具类
 * 
 * @author 高栋梁 dongliangperfect@gmail.com
 */
public class DateUtils {
	public static Date getNextMonth(String ymd){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(ymdStr2Date(ymd).getTime());
		c.add(Calendar.MONTH, 1);
		return new Date(c.getTimeInMillis());
	}
	public static String getMonth(Date date,int month){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date.getTime());
		c.add(Calendar.MONTH, month);
		return ym(new Date(c.getTimeInMillis()));
	}
	/**
	 * 
	 * @param strDate
	 * @param formater
	 * @return
	 */
	public static String findDay(Date d,int days){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(d.getTime());
		c.add(Calendar.DATE, days);
		return ymd(new Date(c.getTimeInMillis()));
	}
	public static Date str2Date(String strDate, String formater) {
		if (strDate == null) {
			return null;
		}
		if (formater == null) {
			formater = "yyyy-MM-dd";
		}
		SimpleDateFormat df = new SimpleDateFormat(formater);
		Date date = new Date();
		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			pe.getStackTrace();
		}
		return date;
	}

	public static Date ymdStr2Date(String strDate) {
		String formater = "yyyy-MM-dd";
		return str2Date(strDate, formater);
	}

	public static Date ymdHmsStr2Date(String strDate) {
		String formater = "yyyy-MM-dd HH:mm:ss";
		return str2Date(strDate, formater);
	}

	public static Date ymdHmStrToDate(String strDate) {
		String formater = "yyyy-MM-dd HH:mm";
		return str2Date(strDate, formater);
	}

	public static String getThisMonth() {
		return formaterDate(new Date(), "yyyy-MM");
	}

	public static String getNextMonth() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DATE, 1);// 设为当前月的1号
		c.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		return formaterDate(c.getTime(), "yyyy-MM");
	}

	public static String getPreviousMonth() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DATE, 1);// 设为当前月的1号
		c.add(Calendar.MONTH, -1);// 减一个月，变为上月的1号
		return formaterDate(c.getTime(), "yyyy-MM");
	}
	/**
	 * 获得 month 的上个月
	 */
	public static String getPreviousMonth(String month) {
		Calendar c = Calendar.getInstance();
		c.setTime(ymdStr2Date(month+"-01"));// 设为当前月的1号
		c.set(Calendar.DATE, 1);// 设为当前月的1号
		c.add(Calendar.MONTH, -1);// 减一个月，变为上月的1号
		return formaterDate(c.getTime(), "yyyy-MM");
	}
	/**
	 * 获取当前星期（中国, 如：星期日,星期一,星期二）
	 */
	public static String cnWeek() {
		Calendar c = GregorianCalendar.getInstance();
		c.setFirstDayOfWeek(Calendar.SUNDAY);
		String[] s = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		return s[c.get(Calendar.DAY_OF_WEEK) - 1];
	}

	/**
	 * 获取当前日期（中国,yyyy年MM月dd日）
	 */
	public static String cnToday() {
		return formaterDate(new Date(), "yyyy年MM月dd日");

	}

	public static String getYear(String ymd) {
		return ymd.substring(0, 4);
	}

	public static String getMonth(String ymd) {
		return ymd.substring(5, 7);
	}

	/**
	 * 将日期字符串加天数转换成新日期字符串
	 * 
	 * @param strDate
	 *            原日期字符串:yyyy-MM-dd
	 * @param days
	 *            增加的天数
	 */
	public static String getNewDayNumDate(String strDate, int days) {
		String[] date = strDate.split("-"); // 将要转换的日期字符串拆分成年月日
		int year, month, day;
		year = Integer.parseInt(date[0]);
		month = Integer.parseInt(date[1]) - 1;
		day = Integer.parseInt(date[2]);
		GregorianCalendar d = new GregorianCalendar(year, month, day);
		d.add(Calendar.DATE, days);
		return DateFormat.getDateInstance().format(d.getTime());
	}

	/**
	 * 从当前时间的字符串格式,带有微秒
	 */
	public static String ymdHmss() {
		Date currentTime = new Date();
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyyMMddHHmmssSSS");
		String dateString = formatter.format(currentTime);
		return dateString;
	}
	/**
	 * 计算两个日期之间相差的天数
	 * @return
	 */
	public static int calDayCount(String startD,String endD){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(ymdStr2Date(endD).getTime());
		int d = c.get(Calendar.DAY_OF_YEAR);
		c.setTimeInMillis(ymdStr2Date(startD).getTime());
		d = d - c.get(Calendar.DAY_OF_YEAR);
		return d+1;
	}
	public static String ymdHmDate(Long time) {
		if (time == null) {
			return "";
		} else {
			if (time <= 0) {
				return "";
			} else {
				return ymdHm(new Date(time));
			}
		}
	}

	public static String[] getTodayStartEnd() {
		String today = DateUtils.getToday();
		return new String[] { today + " 00:00:00", today + " 23:59:59" };
	}

	public static String getPrevOneDay(int day) {
		return "";
	}

	public static String ymd(Date date) {
		return formaterDate(date, null);
	}

	public static String formaterDate(Date date, String formater) {
		if (date == null) {
			return "";
		}
		if (formater == null) {
			formater = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
		return sdf.format(date);
	}

	public static String ymdHm(Date date) {
		return formaterDate(date, "yyyy-MM-dd HH:mm");
	}

	public static String hms(Date date) {
		String s = DateUtils.formaterDate(date, "yyyy-MM-dd HH:mm:ss");
		s = s.substring(11);
		return s.replaceAll(":", "");
	}

	public static String hms() {
		return hms(new Date());
	}

	public static String ym(Date date) {
		return formaterDate(date, "yyyy-MM");
	}

	/**
	 * 获取当前时间的长字符串形式 "yyyy-MM-dd HH:mm:ss"
	 */
	public static String ymdHms(Date date) {
		return formaterDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static String getToday() {
		return formaterDate(new Date(), "yyyy-MM-dd");
	}
	public static String getNow() {
		return formaterDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	public static int getDayNum() {
		return Integer.valueOf(formaterDate(new Date(), "yyyy-MM-dd")
				.split("-")[2]);
	}

	public static String getYestoday() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_YEAR,
				calendar.get(Calendar.DAY_OF_YEAR) - 1);
		return ymd(calendar.getTime());
	}
	public static String getYestoday(String currentDateStr){
		Date d = ymdStr2Date(currentDateStr);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return ymd(calendar.getTime());
	}

	public static String getThisYear() {
		return formaterDate(new Date(), "yyyy");
	}

	public static String getNotFormatToday() {
		return getToday().replaceAll("-", "");
	}

	// 计算当月最后一天,返回字符串
	public static String getMonthLastDay() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DATE, 1);// 设为当前月的1号
		c.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		c.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
		return ymd(c.getTime());
	}

	// 计算指定月最后一天,返回字符串传入每月的第一天
	public static String getMonthLastDay(String ymd) {
		Calendar c = Calendar.getInstance();
		c.setTime(ymdStr2Date(ymd));
		c.set(Calendar.DATE, 1);// 设为当前月的1号
		c.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		c.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
		return ymd(c.getTime());
	}
	// 计算指定月最后一天,返回字符串传入每月的第一天
	public static String getMonthFirstDay(String ymd) {
		Calendar c = Calendar.getInstance();
		c.setTime(ymdStr2Date(ymd));
		c.set(Calendar.DATE, 1);// 设为当前月的1号
		return ymd(c.getTime());
	}

	// 计算当月第一天,返回字符串
	public static String getMonthFirstDay() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DATE, 1);// 设为当前月的1号
		return ymd(c.getTime());
	}

	public static Date[] getMonthStartAndEndDate() {
		Calendar calendar = Calendar.getInstance();
		Date[] dates = new Date[2];
		Date firstDateOfMonth, lastDateOfMonth; // 得到当天是这月的第几天
		int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.add(Calendar.DAY_OF_MONTH, -(dayOfMonth - 1));
		firstDateOfMonth = calendar.getTime();
		calendar.add(Calendar.DAY_OF_MONTH, calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH) - 1);
		lastDateOfMonth = calendar.getTime();
		dates[0] = ymdHmsStr2Date(DateUtils.ymd(firstDateOfMonth) + " 00:00:00");
		dates[1] = ymdHmsStr2Date(DateUtils.ymd(lastDateOfMonth) + " 23:59:59");
		return dates;
	}

	/**
	 * 得到当前日期的下周一0：00：00到下周日23：59：59时间两端的Date
	 * 
	 * @return Date[2]两端的日期
	 */
	public static Date[] getNextWeekDays() {
		return getNextWeekDays(new Date());
	}

	public static String getPrevMonthToday(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(Calendar.MONDAY, c.get(Calendar.MONDAY) - 1);
		return ymdHms(c.getTime());
	}

	public static String getPrevTenDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - 10);
		return ymdHms(c.getTime());
	}

	public static Date[] getLastWeekDays() {
		return getLastWeekDays(new Date());
	}

	/**
	 * 得到当前日期的下周一0：00：00到下周日23：59：59时间两端的Date
	 * 
	 * @param date
	 *            传入的日期
	 * @return Date[2]两端的日期
	 */
	public static Date[] getNextWeekDays(Date date) {
		return getWeekDate(date, 0, 6);
	}

	/**
	 * 得到当前日期的上周一0：00：00到下周日23：59：59时间两端的Date
	 * 
	 * @param date
	 *            传入的日期
	 * @return Date[2]两端的日期
	 */
	public static Date[] getLastWeekDays(Date date) {
		return getWeekDate(date, -14, 6);
	}

	/**
	 * 得到当前日期的本周一0：00：00到下周日23：59：59时间两端的Date
	 * 
	 * @param date
	 *            传入的日期
	 * @return Date[2]两端的日期
	 */
	public static Date[] getThisWeekDays(Date date) {
		return getWeekDate(date, -7, 6);
	}

	public static Date[] getThisWeekDays() {
		return getThisWeekDays(new Date());
	}

	/**
	 * 取得某天所在周的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(String startDate) {
		if (StringUtils.isEmpty(startDate)) {
			return null;
		}
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(DateUtils.ymdStr2Date(startDate));
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
		return c.getTime();
	}

	/**
	 * 取得某天所在周的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(String currentDate) {
		if (StringUtils.isEmpty(currentDate)) {
			return null;
		}
		Calendar c = new GregorianCalendar();
		c.setTime(DateUtils.ymdStr2Date(currentDate));
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
		return c.getTime();
	}

	public static Date[] getWeekDate(Date date, int start, int end) {
		// 将要返回的日期
		Date[] days = new Date[2];

		// 日历
		Calendar calendar = new GregorianCalendar();
		// 设置日历为当前时间
		calendar.setTime(date);
		// 得到今天是周几
		int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
		// 得到当前是全年的第几周
		int weekofYear = calendar.get(Calendar.WEEK_OF_YEAR);
		// 加入不是周日，设置日期到下一周
		if (dayofweek != 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, weekofYear + 1);
		}
		int nextWeekDay = calendar.get(Calendar.DATE);
		// 设置日历为下周第一天,+2是下周从星期一开始，星期一是每周的第2天

		calendar.set(Calendar.DATE, nextWeekDay - dayofweek + 2);
		calendar.add(Calendar.DATE, start);
		days[0] = calendar.getTime();
		String sd = ymd(days[0]) + " 00:00:00";
		days[0] = ymdHmsStr2Date(sd);

		calendar.add(Calendar.DATE, end);
		days[1] = calendar.getTime();
		String ed = ymd(days[1]) + " 23:59:59";
		days[1] = ymdHmsStr2Date(ed);
		return days;
	}

	/**
	 * 获得前十天时间
	 * 
	 * @param date
	 * @return
	 */
	public static String getPrevTenDay(int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - day);
		return ymdHms(c.getTime());
	}
	/**
	 * 获得前一个时间的前几天时间
	 * 
	 * @param date
	 * @return
	 */
	public static String getPrevTenDay(String date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(DateUtils.ymdStr2Date(date));
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - day);
		return ymd(c.getTime());
	}

	/**
	 * 获得前一个时间的后几天时间
	 * 
	 * @param date
	 * @return
	 */
	public static String getAfterSomeDayYms(String date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(DateUtils.ymdStr2Date(date));
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + day);
		return ymd(c.getTime());
	}

	/**
	 * 
	 * @param date1
	 * @param date2
	 * @return 2个日期是否属于同一周
	 */
	public static boolean isSameWeek(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (0 == subYear) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR)) {
				return true;
			}
		} else if ((1 == subYear) && (11 == cal2.get(Calendar.MONTH))) {
			// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR)) {
				return true;
			}
		} else if ((-1 == subYear) && (11 == cal1.get(Calendar.MONTH))) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR)) {
				return true;
			}
		}
		return false;
	}

	public static String getMonthName(String month) {
		String monthname = "december";
		if ("01".equals(month)) {
			monthname = "january";
		} else if ("02".equals(month)) {
			monthname = "february";
		} else if ("03".equals(month)) {
			monthname = "march";
		} else if ("04".equals(month)) {
			monthname = "april";
		} else if ("05".equals(month)) {
			monthname = "may";
		} else if ("06".equals(month)) {
			monthname = "june";
		} else if ("07".equals(month)) {
			monthname = "july";
		} else if ("08".equals(month)) {
			monthname = "august";
		} else if ("09".equals(month)) {
			monthname = "september";
		} else if ("10".equals(month)) {
			monthname = "october";
		} else if ("11".equals(month)) {
			monthname = "november";
		}
		return monthname;
	}

	public static String[] getSeason(String month) {
		String[] seasonTime = new String[] { "", "", "", "" };
		if ("01".equals(month) || "02".equals(month) || "03".equals(month)) {
			seasonTime[0] = "01-01";
			seasonTime[1] = "03-31";
			seasonTime[2] = "january+february+march";
			seasonTime[3] = "第一季度";
		} else if ("04".equals(month) || "05".equals(month)
				|| "06".equals(month)) {
			seasonTime[0] = "04-01";
			seasonTime[1] = "06-30";
			seasonTime[2] = "april+may+june";
			seasonTime[3] = "第二季度";
		} else if ("07".equals(month) || "08".equals(month)
				|| "09".equals(month)) {
			seasonTime[0] = "07-01";
			seasonTime[1] = "03-30";
			seasonTime[2] = "july+august+september";
			seasonTime[3] = "第三季度";
		} else if ("10".equals(month) || "11".equals(month)
				|| "12".equals(month)) {
			seasonTime[0] = "10-01";
			seasonTime[1] = "12-31";
			seasonTime[2] = "october+november+december";
			seasonTime[3] = "第四季度";
		}
		return seasonTime;
	}

	/**
	 * 得到一个日期是周几
	 */
	public static String getWeekCS(Date date) {

		Calendar c = GregorianCalendar.getInstance();
		c.setTime(date);
		c.setFirstDayOfWeek(Calendar.SUNDAY);
		String[] s = { "sunday", "monday", "tuesday", "wednesday", "thursday",
				"friday", "saturday" };
		return s[c.get(Calendar.DAY_OF_WEEK) - 1];
	}

	/**
	 * 根据两个日期段得到这个时间段所有的周一和周日的日期
	 */
	public static List getWeek(String startDate, String endDate)
			throws Exception {
		if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
			return new ArrayList();
		}
		List<String> dateList = new ArrayList<String>();
		// 得开始日期所在的周一，和结束日期所在的周末
		startDate = ymd(getFirstDayOfWeek(startDate));
		endDate = ymd(getLastDayOfWeek(endDate));
		String date;
		// 得到两个日期之间有多少周
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
				Locale.ENGLISH); // 设定格式
		dateFormat.setLenient(false);
		Date start = dateFormat.parse(startDate);
		Date end = dateFormat.parse(endDate);
		long temp = ((end.getTime() - start.getTime()) / 1000);// 秒数
		temp = temp / (60 * 60 * 24); // 天数
		temp = temp / 7; // 周数
		// 根据周数将每周开始日期和结束日期打包
		int j = -1;
		for (int i = 0; i <= temp; i++) {
			j = j + 1;
			date = getAfterSomeDayYms(startDate, j);
			j = j + 6;
			date = date + "~" + getAfterSomeDayYms(startDate, j);
			dateList.add(date);
		}
		// 对结果进行倒序排序使得最近的日期在上边
		Collections.reverse(dateList);
		return dateList;
	}

	public static String getTomorrow() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_YEAR,
				calendar.get(Calendar.DAY_OF_YEAR) + 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(calendar.getTime());
	}
	public static String getTomorrow(String ymd) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(ymdStr2Date(ymd));
		calendar.set(Calendar.DAY_OF_YEAR,
				calendar.get(Calendar.DAY_OF_YEAR) + 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(calendar.getTime());
	}
	@SuppressWarnings("unchecked")
	public static List makeHourList() {
		List list = new ArrayList();
		for (int i = 0; i < 24; i++) {
			if (i < 10) {
				list.add((new StringBuilder("0")).append(i).toString());
			} else {
				list.add(String.valueOf(i));
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public static List makeMininuteList() {
		List list = new ArrayList();
		for (int i = 0; i < 6; i++) {
			list.add((new StringBuilder(String.valueOf(String.valueOf(i))))
					.append("0").toString());
		}
		return list;
	}

	public static String makeHour(Date endDate) {
		String hour = "";
		if (endDate != null) {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String after = format.format(endDate);
			hour = after.substring(11, 13);
		}
		return hour;
	}

	public static String makeMini(Date endDate) {
		String mini = "";
		if (endDate != null) {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String after = format.format(endDate);
			mini = after.substring(14, 16);
		}
		return mini;
	}

	public static List getTomorrowList() {
		List<String> dateList = new ArrayList<String>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String tomorrow = sdf.format(calendar.getTime());
		dateList.add(tomorrow);
		for (int i = 0; i < 6; i++) {
			calendar.set(6, calendar.get(6) + 1);
			tomorrow = sdf.format(calendar.getTime());
			dateList.add(tomorrow);
		}

		return dateList;
	}

	public static boolean getstartDateVal() {
		Date d = new Date();
		String sd = ymd(d) + " 00:00:00";
		String sd1 = ymd(d) + " 00:00:15";
		if (ymdHmsStr2Date(sd).getTime() <= d.getTime()
				&& d.getTime() < ymdHmsStr2Date(sd1).getTime()) {
			return true;
		} else {
			return false;
		}

	}

	public static String getWeekStartDateString(String startDate) {
		return getWeekDateString(startDate, 0);
	}

	public static String getWeekEndDateString(String endDate) {
		return getWeekDateString(endDate, 6);
	}

	public static String getWeekDateString(String currentDate, int day) {
		// 日历
		Calendar calendar = new GregorianCalendar();
		// 设置日历为当前时间
		calendar.setTime(ymdStr2Date(currentDate));
		// 得到今天是周几
		int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
		int weekofYear = calendar.get(Calendar.WEEK_OF_YEAR);
		// 加入不是周日，设置日期到下一周
		if (dayofweek == 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, weekofYear - 1);
		}
		int nextWeekDay = calendar.get(Calendar.DATE);
		// 设置日历为下周第一天,+2是下周从星期一开始，星期一是每周的第2天
		calendar.set(Calendar.DATE, nextWeekDay - dayofweek + 2);
		calendar.add(Calendar.DATE, day);
		return ymd(calendar.getTime());
	}

	public static Date[] getWeekStartAndEndDate(Date currentDate, int start) {
		// 将要返回的日期
		Date[] days = new Date[2];

		// 日历
		Calendar calendar = new GregorianCalendar();
		// 设置日历为当前时间
		calendar.setTime(currentDate);
		// 得到今天是周几
		int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
		int weekofYear = calendar.get(Calendar.WEEK_OF_YEAR);
		// 加入不是周日，设置日期到下一周
		if (dayofweek == 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, weekofYear - 1);
		}
		// 得到当前是全年的第几周
		int nextWeekDay = calendar.get(Calendar.DATE);
		// 设置日历为下周第一天,+5是下周从星期四开始，星期四是每周的第5天
		calendar.set(Calendar.DATE, nextWeekDay - dayofweek + 2);
		calendar.add(Calendar.DATE, start);
		days[0] = calendar.getTime();
		days[0] = ymdHmsStr2Date(ymd(days[0]) + " 00:00:00");
		calendar.add(Calendar.DATE, 6);
		days[1] = calendar.getTime();
		days[1] = ymdHmsStr2Date(ymd(days[1]) + " 23:59:59");
		return days;
	}

	public static Date[] getWeekOneToFiveDate(Date currentDate, int start) {
		// 将要返回的日期
		Date[] days = new Date[2];

		// 日历
		Calendar calendar = new GregorianCalendar();
		// 设置日历为当前时间
		calendar.setTime(currentDate);
		// 得到今天是周几
		int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
		int weekofYear = calendar.get(Calendar.WEEK_OF_YEAR);
		// 加入不是周日，设置日期到下一周
		if (dayofweek == 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, weekofYear - 1);
		}
		// 得到当前是全年的第几周
		int nextWeekDay = calendar.get(Calendar.DATE);
		// 设置日历为下周第一天,+5是下周从星期四开始，星期四是每周的第5天
		calendar.set(Calendar.DATE, nextWeekDay - dayofweek + 2);
		calendar.add(Calendar.DATE, start);
		days[0] = calendar.getTime();
		days[0] = ymdHmsStr2Date(ymd(days[0]) + " 00:00:00");
		calendar.add(Calendar.DATE, 4);
		days[1] = calendar.getTime();
		days[1] = ymdHmsStr2Date(ymd(days[1]) + " 23:59:59");
		return days;
	}

	public static String[] getWeekTime(String date) {
		// 将要返回的日期
		String[] days = new String[7];

		// 日历
		Calendar calendar = new GregorianCalendar();
		// 设置日历为当前时间
		calendar.setTime(ymdStr2Date(date));
		// 得到今天是周几
		int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
		int weekofYear = calendar.get(Calendar.WEEK_OF_YEAR);
		// 加入不是周日，设置日期到下一周
		if (dayofweek == 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, weekofYear - 1);
		}
		// 得到当前是全年的第几周
		int nextWeekDay = calendar.get(Calendar.DATE);
		calendar.set(Calendar.DATE, nextWeekDay - dayofweek + 2);
		calendar.add(Calendar.DATE, 0);
		days[0] = ymd(calendar.getTime());
		for (int i = 1; i < 7; i++) {
			calendar.set(Calendar.DAY_OF_YEAR, calendar
					.get(Calendar.DAY_OF_YEAR) + 1);
			days[i] = ymd(calendar.getTime());
		}
		return days;
	}

	// 起始周几-结束周几
	public static String[] getWeekTimeByNum(String startDate, String endDate) {
		// 将要返回的日期
		String[] days = new String[2];

		// 日历
		Calendar calendar = new GregorianCalendar();
		// 设置日历为当前时间
		calendar.setTime(ymdStr2Date(startDate));
		// 得到今天是周几
		int startDayofweek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		calendar.setTime(ymdStr2Date(endDate));
		// 得到今天是周几
		int endDayofweek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		days[0] = startDayofweek + "";
		if (endDayofweek == 0) {
			endDayofweek = 7;
		}
		days[1] = endDayofweek + "";
		return days;
	}

	public static String[] getWeekStartAndEndDate(String currentDate, int start) {
		// 将要返回的日期
		String[] days = new String[2];

		// 日历
		Calendar calendar = new GregorianCalendar();
		// 设置日历为当前时间
		calendar.setTime(ymdStr2Date(currentDate));
		// 得到今天是周几
		int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
		int weekofYear = calendar.get(Calendar.WEEK_OF_YEAR);
		// 加入不是周日，设置日期到下一周
		if (dayofweek == 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, weekofYear - 1);
		}
		// 得到当前是全年的第几周
		int nextWeekDay = calendar.get(Calendar.DATE);
		calendar.set(Calendar.DATE, nextWeekDay - dayofweek + 2);
		calendar.add(Calendar.DATE, start);
		days[0] = ymd(calendar.getTime());
		calendar.add(Calendar.DATE, 6);
		days[1] = ymd(calendar.getTime());
		return days;
	}

	public static String[] getThisWeekStartEndDays() {
		return getWeekStartAndEndDate(ymd(new Date()), 0);
	}

	public static String[] getLastWeekStartEndDays() {
		return getWeekStartAndEndDate(ymd(new Date()), -7);
	}

	public static String[] getNextWeekStartEndDays() {
		return getWeekStartAndEndDate(ymd(new Date()), 7);
	}

	public static List getWeekDateList(String date) throws ParseException {
		List<String> dateList = new ArrayList<String>();
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
		Date dateVar = ymd.parse(date);
		calendar.setTime(dateVar);
		dateList.add(date);
		for (int i = 1; i <= 6; i++) {
			calendar.set(6, calendar.get(6) + 1);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String tomorrow = sdf.format(calendar.getTime());
			dateList.add(tomorrow);
		}
		return dateList;
	}

	public static String getNextDay(String currentDate) {
		Date d = ymdStr2Date(currentDate);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 1);
		return ymd(c.getTime());
	}
	public static String getPreviousDay(String currentDate) {
		Date d = ymdStr2Date(currentDate);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - 1);
		return ymd(c.getTime());
	}

	public static Date getNextDay(Date currentDate) {
		Date d = currentDate;
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 1);
		return c.getTime();
	}

	/**
	 * 获得开始时间和结束时间之间的时间列表
	 * 
	 * @param startDate
	 * @param endDate
	 * @return List<String>
	 * @throws ParseException
	 */
	public static List getDateList(String startDate, String endDate)
			throws ParseException {
		List<String> dateList = new ArrayList<String>();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
				Locale.ENGLISH); // 设定格式
		Date start = dateFormat.parse(startDate);
		Date end = dateFormat.parse(endDate);
		long day = ((end.getTime() - start.getTime()) / 1000);// 秒数
		day = day / (60 * 60 * 24); // 天数
		for (int i = 0; i <= day; i++) {
			String date = getAfterSomeDayYms(startDate, i);
			dateList.add(date);
		}
		return dateList;
	}

	// 20090623
	public static int compareDates(Date firstDate, Date secondDate) {
		int result = 0;
		if (firstDate.compareTo(secondDate) > 0) {
			result = 1;
		} else if (firstDate.compareTo(secondDate) < 0) {
			result = -1;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public static List getweekDatesByMonthStr(String ym) {
		List l = new ArrayList();
		// 月的第一周
		String d = null;
		if (null == ym || "".equals(ym)) {
			d = getMonthFirstDay();
		} else {
			d = ym + "-01";
		}
		
		String[] w1 = getWeekStartAndEndDate(d, 0);
		
		if(w1[1].equals(d)){
			w1 = getWeekStartAndEndDate(getNextDay(d), 0);
		}else if(getPreviousDay(w1[1]).equals(d)){
			w1 = getWeekStartAndEndDate(getNextDay(getNextDay(d)), 0);
		}else{
			w1[0] = d;
		}
		d = getNextDay(w1[1]);
		String[] w2 = getWeekStartAndEndDate(d, 0);
		d = getNextDay(w2[1]);
		String[] w3 = getWeekStartAndEndDate(d, 0);
		d = getNextDay(w3[1]);
		String[] w4 = getWeekStartAndEndDate(d, 0);
		boolean tag = false;
		if(ymdStr2Date(w4[1]).getTime() < ymdStr2Date(getMonthLastDay(d)).getTime()){
			tag = true ;
		}
		
		if(!tag){
			w4[1] = getMonthLastDay(d);
		}
		l.add(w1);
		l.add(w2);
		l.add(w3);
		l.add(w4);
		if(tag){
			d = getNextDay(w4[1]);
			String[] w5 = getWeekStartAndEndDate(d, 0);
			w5[1] = getMonthLastDay(d);
			l.add(w5);
		}
		return l;
	}

	@SuppressWarnings("unchecked")
	public static List getweekDatesByMonth() {
		List l = new ArrayList();
		// 月的第一周
		Date d = ymdStr2Date(getMonthFirstDay());
		Date[] w1 = getWeekStartAndEndDate(d, 0);
		w1[0] = d;
		d = getNextDay(w1[1]);
		Date[] w2 = getWeekStartAndEndDate(d, 0);
		d = getNextDay(w2[1]);
		Date[] w3 = getWeekStartAndEndDate(d, 0);
		d = getNextDay(w3[1]);
		Date[] w4 = getWeekStartAndEndDate(d, 0);
		d = getNextDay(w4[1]);
		Date[] w5 = getWeekStartAndEndDate(d, 0);
		w5[1] = ymdStr2Date(getMonthLastDay());
		l.add(w1);
		l.add(w2);
		l.add(w3);
		l.add(w4);
		l.add(w5);
		return l;
	}

	@SuppressWarnings("unchecked")
	public static List getweekDatesByMonth1(String date) {
		List l = new ArrayList();
		String d1 = getMonthFirstDay(date);
		String[] w = getWeekStartAndEndDate(d1, 0);
		if(w[1].equals(d1)){
			d1 = getNextDay(d1);
		}else if(getPreviousDay(w[1]).equals(d1)){
			d1 = getNextDay(getNextDay(d1));
		}
		// 月的第一周
		Date d = ymdStr2Date(d1);
		Date[] w1 = getWeekOneToFiveDate(d, 0);
		Date[] ww1 = getWeekStartAndEndDate(d, 0);
		w1[0] = d;
		d = getNextDay(ww1[1]);
		Date[] w2 = getWeekOneToFiveDate(d, 0);
		Date[] ww2 = getWeekStartAndEndDate(d, 0);
		d = getNextDay(ww2[1]);
		Date[] w3 = getWeekOneToFiveDate(d, 0);
		Date[] ww3 = getWeekStartAndEndDate(d, 0);
		d = getNextDay(ww3[1]);
		Date[] w4 = getWeekOneToFiveDate(d, 0);
		Date[] ww4 = getWeekStartAndEndDate(d, 0);
		boolean tag = false;
		if(ww4[1].getTime() < ymdStr2Date(getMonthLastDay(d1)).getTime()){
			tag = true;
		}
		if(!tag){
			w4[1] = ymdStr2Date(getMonthLastDay());
		}
		l.add(w1);
		l.add(w2);
		l.add(w3);
		l.add(w4);
		if(tag){
			d = getNextDay(ww4[1]);
			Date[] w5 = getWeekOneToFiveDate(d, 0);
			if (w5[1].getTime() > ymdStr2Date(getMonthLastDay()).getTime()) {
				w5[1] = ymdStr2Date(getMonthLastDay());
			}
			l.add(w5);
		}
		return l;
	}

	@SuppressWarnings("unchecked")
	public static String getWeekNum(Date nowDate) {
		String weekNum = "";
		List<Date[]> w = getweekDatesByMonth1(ymd(nowDate));
		if (nowDate.getTime() >= w.get(0)[0].getTime()
				&& nowDate.getTime() <= w.get(0)[1].getTime()) {
			weekNum = "01";
//		} else if (nowDate.getTime() >= w.get(0)[1].getTime()
//				&& nowDate.getTime() <= w.get(1)[0].getTime()) {
//			weekNum = "12";
		} else if (nowDate.getTime() >= w.get(1)[0].getTime()
				&& nowDate.getTime() <= w.get(1)[1].getTime()) {
			weekNum = "02";
//		} else if (nowDate.getTime() >= w.get(1)[1].getTime()
//				&& nowDate.getTime() <= w.get(2)[0].getTime()) {
//			weekNum = "23";
		} else if (nowDate.getTime() >= w.get(2)[0].getTime()
				&& nowDate.getTime() <= w.get(2)[1].getTime()) {
			weekNum = "03";
//		} else if (nowDate.getTime() >= w.get(2)[1].getTime()
//				&& nowDate.getTime() <= w.get(3)[0].getTime()) {
//			weekNum = "34";
		} else if (nowDate.getTime() >= w.get(3)[0].getTime()
				&& nowDate.getTime() <= w.get(3)[1].getTime()) {
			weekNum = "04";
//		} else if (w.get(4) != null
//				&& nowDate.getTime() >= w.get(3)[1].getTime()
//				&& nowDate.getTime() <= w.get(4)[0].getTime()) {
//			weekNum = "45";
		} else if (w.get(4) != null
				&& nowDate.getTime() >= w.get(4)[0].getTime()
				&& nowDate.getTime() <= w.get(4)[1].getTime()) {
			weekNum = "05";
		}

		return weekNum;
	}

	public static void main(String args[]) {
		String[] ss=getWeekTime(ymd(new Date()));
			System.out.println(ss[0]+ss[ss.length-1]);
		
	}
}
